package com.fx.smsclient.JsonSerializer;

import com.google.gson.*;
import com.zx.sms.codec.cmpp.msg.CmppSubmitRequestMessage;
import com.zx.sms.codec.cmpp.msg.CmppSubmitResponseMessage;

import java.lang.reflect.Type;

public class CmppSubmitResponseMessageSerializer implements JsonSerializer<CmppSubmitResponseMessage> {

    static Gson gson = new GsonBuilder().create();

    @Override
    public JsonElement serialize(CmppSubmitResponseMessage src, Type typeOfSrc, JsonSerializationContext context) {
        JsonElement jsonElement = gson.toJsonTree(src);
        JsonObject jsonObject = (JsonObject) jsonElement;
        jsonObject.remove("msgId");
        jsonObject.addProperty("msgId",src.getMsgId().toString());
//       jsonObject.remove("request");
        if (jsonObject.has("request")){
            jsonObject.getAsJsonObject("request").remove("msgid");
            CmppSubmitRequestMessage cmppSubmitRequestMessage =(CmppSubmitRequestMessage) src.getRequest();
            jsonObject.getAsJsonObject("request").addProperty("msgId",cmppSubmitRequestMessage.getMsgid().toString());
            jsonObject.getAsJsonObject("request").remove("msgContentBytes");
            jsonObject.getAsJsonObject("request").addProperty("msgContent",cmppSubmitRequestMessage.getMsgContent());
            jsonObject.getAsJsonObject("request").remove("header");
            jsonObject.getAsJsonObject("request").remove("buffer");
        }
        jsonObject.remove("header");
        jsonObject.addProperty("commandId",Integer.toUnsignedLong(src.getHeader().getCommandId()));
        jsonObject.addProperty("sequenceId",src.getHeader().getSequenceId());
        return jsonObject;
    }
}
